package cn.px.rest.controller.sqarepartmanage;

import cn.px.dao.entity.sqarepartmanage.SparePartInfo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 批量导入用户
 *
 * 备件导入
 *
 */
public class SpareInfo {
    private static String PATH = "G:\\depository\\sb\\spring-boot-sb-base\\platform-rest\\src\\test\\java\\com\\test\\";
    private static String file = PATH + "tb_hp-库存商品-20201111.txt";
    public static void main(String[] args) {
        getList();
    }
    public static List<SparePartInfo> getList() {

        /* 读取数据 */
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(file)),
                    "UTF-8"));
            String lineTxt = null;
            List<String> banks = new ArrayList<String>();
            while ((lineTxt = br.readLine()) != null) {
                if(!banks.contains(lineTxt)){
                    banks.add(lineTxt);
                }
            }
            br.close();
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date now = new Date();
            List<SparePartInfo> list = new ArrayList<SparePartInfo>();
            int i = 0;
            for(String bank:banks){
                SparePartInfo info = new SparePartInfo();
                String[] line = bank.split("\t");
                info.setId(line[0]);
                if(line[1].trim().contains("配件")){
                    info.setYt(1);
                }else if(line[1].trim().contains("劳保用品")){
                    info.setYt(4);
                }else if(line[1].trim().contains("五金工具")){
                    info.setYt(3);
                }else{
                    info.setYt(2);
                }
                String typeName = line[1].trim().replace("配件\\","")
                        .replace("配件/","")
                        .replace("钢材\\","")
                        .replace("辅油\\","")
                        .replace("五金工具\\","");
                info.setTypeId(typeName);
                info.setLevel(1);
                info.setCreatedTime(LocalDateTime.now());
                info.setName(line[3]);
                info.setNo(line[4]);

                if(line.length>5){
                    info.setGgxh(line[5]);
                    info.setUnit(line[6]);
                    info.setZjm(line[7]);
                }else if(line.length == 5){
                    info.setName(line[1]);
                }
                list.add(info);
            }

            System.out.println(list.size());
            return list;
        } catch (Exception e) {
            System.err.println("read errors :" + e);
        }
        return null;
    }
}
